package com.trs.listviewandcheckbox;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ViewUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   private ListView listView;
    private Button btn_delete;
    private Button btn_bianij;
    private Button btn_clear;
    public Boolean editMode=false;
    private MyListViewAdapter myListViewAdapter;

    SparseArray<Boolean> checkStates;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initDate();
       initView();

    }
    private void initView() {
        listView= (ListView) findViewById(R.id.lv_listview);
        btn_delete= (Button) findViewById(R.id.btn_delete);
        btn_bianij= (Button) findViewById(R.id.btn_bianij);
        btn_clear= (Button) findViewById(R.id.btn_clear);
        btn_delete.setOnClickListener(this);
        btn_bianij.setOnClickListener(this);
        btn_clear.setOnClickListener(this);
         myListViewAdapter = new MyListViewAdapter(this/*,editMode*/);
        listView.setAdapter(myListViewAdapter);

    }
    private void initDate() {
        checkStates = new SparseArray<>();
        for (int i = 0; i < 100; i++) {
            checkStates.put(i, false);
        }
    }
    @Override
    public void onClick(View v) {
          switch (v.getId()){
              case R.id.btn_delete:
                  for (int i=0;i<checkStates.size();i++){
                      if (checkStates.valueAt(i)){
                          checkStates.delete(checkStates.keyAt(i));
//                当前的项已被删除，记得i要自减，否则会出现混乱
                          i--;
                      }
                  }
                  myListViewAdapter.notifyDataSetChanged();
                  break;
              case R.id.btn_bianij:
                  btn_bianij.setVisibility(View.GONE);
                  btn_delete.setVisibility(View.VISIBLE);
                  btn_clear.setVisibility(View.VISIBLE);
                  if (editMode) {    //如果是编辑模式
                      editMode = false;     //点击按钮后切换到非编纪模式
                  } else {
                      editMode = true;
                  }
                  myListViewAdapter.notifyDataSetChanged();    //通知ListView更新界面
                  break;
              case R.id.btn_clear:


                  break;
          }
    }

    class MyListViewAdapter extends BaseAdapter {
        // 用来控制CheckBox的选中状况
        private  HashMap<Integer, Boolean> isSelected;
        LayoutInflater inflater;
//        private boolean editMode;
        public MyListViewAdapter(Context mContext/*,Boolean editMode*/){
//            this.editMode=editMode;
            inflater =LayoutInflater.from(mContext);
            isSelected = new HashMap<Integer, Boolean>();
            // 初始化数据
//            initDate();
        }
//         初始化isSelected的数据
//        private void initDate() {
//            for (int i = 0; i < checkStates.size(); i++) {
//                getIsSelected().put(i, false);
//            }
//        }
        @Override
        public int getCount() {
            return checkStates.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.list_item, parent,false);
                holder = new ViewHolder();
                holder.cb_check= (CheckBox) convertView.findViewById(R.id.cb_check);
                holder.tv_text= (TextView) convertView.findViewById(R.id.tv_text);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            if (editMode) {
                holder.cb_check.setVisibility(View.VISIBLE);
            } else {
                holder.cb_check.setVisibility(View.GONE);
            }
            holder.cb_check.setText(checkStates.keyAt(position)+"");

//            用户点击checkbox行为会需要增加监听来改变checkStates对应项的状态
            holder.cb_check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    checkStates.setValueAt(position, isChecked);
                }
            });
            holder.cb_check.setChecked(checkStates.valueAt(position));
//            holder.cb_check.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if (isSelected.get(position)) {
//                        isSelected.put(position, false);
//                        setIsSelected(isSelected);
//                    } else {
//                        isSelected.put(position, true);
//                        setIsSelected(isSelected);
//                    }
//                }
//            });
//            holder.cb_check.setChecked(getIsSelected().get(position));
            return convertView;
        }
//        public  HashMap<Integer, Boolean> getIsSelected() {
//            return isSelected;
//        }
//
//        public  void setIsSelected(HashMap<Integer, Boolean> isSelected) {
//            this.isSelected = isSelected;
//        }
    }
    class ViewHolder{
       CheckBox cb_check;
        TextView tv_text;

    }

}
